import axios from 'axios'
import type { ILoginRequestParams } from '@/manager/types'
import type { IChatMessageItem, PlainObject } from '@/common/types'
import { UserTypeEnum } from '@/common/constants'

const request = axios.create({
  baseURL: 'api/beauty_ssm'
})

request.interceptors.request.use((config) => {
  return config
})

request.interceptors.response.use((response) => {
  return response.data
})

export const login = (data: ILoginRequestParams) => {
  return request.post('api/service/login/doLogin', data)
}

export const getVisitorList = (serviceId: string) => {
  return request.post('nowService/selectMySevice', {
    kefuCode: serviceId,
    pageSize: 100,
    pageNum: 1
  })
}

export const requestMessageData = async (data: object) => {
  const res = await request.post('v2ChatLog/selectChatRecordByFkCode', {
    kefuCode: data.serviceId,
    fangkeCode: data.visitorId,
    pageSize: 50,
    pageNum: data.pageNum
  })

  const list = res.data.list.map((item: object) => {
    return <IChatMessageItem>{
      from: item.fromType === 'KF' ? UserTypeEnum.SERVICE : UserTypeEnum.VISITOR,
      id: item.toId,
      name: item.toName ?? '',
      avatar: item.toAvatar ?? '',
      content: item.content,
      read: item.readFlag,
      time: item.createTime
    }
  })
  return {
    list
  }
}
